﻿using System.Collections;
using System.Collections.Generic;
using System.Threading;
using UnityEngine;

public class LGClientProtocol : MonoBehaviour
{
    const int sleepTime = 500;
    [SerializeField]
    RRClient rRClient;

    const string On00 = "01 05 00 00 FF 00 8C 3A";
    const string Off00 = "01 05 00 00 00 00 CD CA";
    const string On01 = "01 05 00 01 FF 00 DD FA";
    const string Off01 = "01 05 00 01 00 00 9C 0A";
    const string On02 = "01 05 00 02 FF 00 2D FA";
    const string Off02 = "01 05 00 02 00 00 6C 0A";
    const string On03 = "01 05 00 03 FF 00 7C 3A";
    const string Off03 = "01 05 00 03 00 00 3D CA";
    const string On04 = "01 05 00 04 FF 00 CD FB";
    const string Off04 = "01 05 00 04 00 00 8C 0B";
    const string On05 = "01 05 00 05 FF 00 9C 3B";
    const string Off05 = "01 05 00 05 00 00 DD CB";
    const string On06 = "01 05 00 06 FF 00 6C 3B";
    const string Off06 = "01 05 00 06 00 00 2D CB";
    const string On07 = "01 05 00 07 FF 00 3D FB";
    const string Off07 = "01 05 00 07 00 00 7C 0B";
    const string On08 = "01 05 00 08 FF 00 0D F8";
    const string Off08 = "01 05 00 08 00 00 4C 08";
    const string On09 = "01 05 00 09 FF 00 5C 38";
    const string Off09 = "01 05 00 09 00 00 1D C8";
    const string On0A = "01 05 00 0A FF 00 AC 38";
    const string Off0A = "01 05 00 0A 00 00 ED C8";
    const string On0B = "01 05 00 0B FF 00 FD F8";
    const string Off0B = "01 05 00 0B 00 00 BC 08";
    const string On0C = "01 05 00 0C FF 00 4C 39";
    const string Off0C = "01 05 00 0C 00 00 0D C9";
    const string On0D = "01 05 00 0D FF 00 1D F9";
    const string Off0D = "01 05 00 0D 00 00 5C 09";
    const string On0E = "01 05 00 0E FF 00 ED F9";
    const string Off0E = "01 05 00 0E 00 00 AC 09";
    const string On0F = "01 05 00 0F FF 00 BC 39";
    const string Off0F = "01 05 00 0F 00 00 FD C9";
    const string On10 = "01 05 00 10 FF 00 8D FF";
    const string Off10 = "01 05 00 10 00 00 CC 0F";
    const string On11 = "01 05 00 11 FF 00 DC 3F";
    const string Off11 = "01 05 00 11 00 00 9D CF";
    const string On12 = "01 05 00 12 FF 00 2C 3F";
    const string Off12 = "01 05 00 12 00 00 6D CF";


    const string PowerOnPC = "AE 20 CF 29 03 FF FF 00 AC";
    const string PowerOffPC = "AE 20 CF 29 03 FF 00 00 AC";

    const string PowerOnTouYing = "EB 90 00 0C 00 00 08 02 00 01 01 93";
    const string PowerOffTouYing = "EB 90 00 0C 00 00 08 01 00 01 00 91";

    /// <summary>
    /// 手动断开后需要解锁才能远控
    /// </summary>
    /// <param name="dataTypeEnum"></param>
    /// <param name="id"></param>
    public void unlock(DataTypeEnum dataTypeEnum, string id)
    {
        //01 06 08 00 00 C3 CB FB
        //01 06 08 01 00 C3 9A 3B
        byte[] hexdata = ConvertUtil.HexStrTobyte($"01 06 08 {id} 00 C3");
        byte[] crcdatas = CRC.GetCRCDatas(hexdata);
        rRClient.SendBytes(dataTypeEnum, OrderTypeEnum.Str, crcdatas);
    }

    /// <summary>
    /// 打开开关
    /// </summary>
    /// <param name="dataTypeEnum"></param>
    /// <param name="id"></param>
    public void openswitch(DataTypeEnum dataTypeEnum, string id)
    {
        //const string On0B = "01 05 00 0B FF 00 FD F8";
        //const string Off0B = "01 05 00 0B 00 00 BC 08";
        byte[] hexdata = ConvertUtil.HexStrTobyte($"01 05 00 {id} FF 00");
        byte[] crcdatas = CRC.GetCRCDatas(hexdata);
        rRClient.SendBytes(dataTypeEnum, OrderTypeEnum.Str, crcdatas);
    }
    /// <summary>
    /// 关闭开关
    /// </summary>
    /// <param name="dataTypeEnum"></param>
    /// <param name="id"></param>
    public void closeswitch(DataTypeEnum dataTypeEnum, string id)
    {
        //const string On0B = "01 05 00 0B FF 00 FD F8";
        //const string Off0B = "01 05 00 0B 00 00 BC 08";
        byte[] hexdata = ConvertUtil.HexStrTobyte($"01 05 00 {id} 00 00");
        byte[] crcdatas = CRC.GetCRCDatas(hexdata);
        rRClient.SendBytes(dataTypeEnum, OrderTypeEnum.Str, crcdatas);
    }

    /// <summary>
    /// 开关组 c1的二进制数据从右到左为对应8-15号开关状态  c2对应0-7号开关 1为开 0为关
    /// </summary>
    /// <param name="dataTypeEnum"></param>
    /// <param name="c1"></param>
    /// <param name="c2"></param>
    public void SetSwitchGroup(DataTypeEnum dataTypeEnum, string c1, string c2)
    {
        byte[] hexdata = ConvertUtil.HexStrTobyte($"01 0F 00 00 00 10 02 {c2} {c1}"); //10 为 一次控制15个空开  = 0x0F+1
        byte[] crcdatas = CRC.GetCRCDatas(hexdata);
        rRClient.SendBytes(dataTypeEnum, OrderTypeEnum.Str, crcdatas);
    }

    /// <summary>
    /// 智能开关全开
    /// </summary>
    public void OpenAllSwitch()
    {
        SetSwitchGroup(DataTypeEnum.LG20001, "FF", "FF");
        SetSwitchGroup(DataTypeEnum.LG20002, "FF", "FF");
    }

    /// <summary>
    /// 智能开关全关
    /// </summary>
    public void CloseAllSwitch()
    {
        SetSwitchGroup(DataTypeEnum.LG20001, "00", "00");
        SetSwitchGroup(DataTypeEnum.LG20002, "00", "00");
    }

    /// <summary>
    /// 一键开馆
    /// </summary>
    public void KaiGuan()
    {
        Loom.RunAsync(() =>
        {
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off10);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off11);
            SetSwitchGroup(DataTypeEnum.LG20001, "FC", "33");
            Thread.Sleep(sleepTime);
            SetSwitchGroup(DataTypeEnum.LG20002, "D7", "EF");

            Thread.Sleep(5000);
            OnTouYing();
            Thread.Sleep(2000);
            OnTouYing();
            Thread.Sleep(1000);
            OnTouYing();
            Thread.Sleep(1000);
            OpenAllPC();
            Thread.Sleep(1000);
            OnTouYing();
        });
    }

    /// <summary>
    /// 一键闭馆
    /// </summary>
    public void BiGuan()
    {
        //ConfirmPanel.ins.Show("确认闭馆?", () =>
        //{
        //    Loom.RunAsync(() =>
        //    {
        //        OffTouYing();
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off11);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off10);
        //        SetSwitchGroup(DataTypeEnum.LG20001, "0C", "00");
        //        Thread.Sleep(sleepTime);
        //        OffTouYing();
        //        SetSwitchGroup(DataTypeEnum.LG20002, "00", "89");
        //        //关主机
        //        rRClient.Send(DataTypeEnum.ShutDown, OrderTypeEnum.ShutDown, "");
        //        Thread.Sleep(10000);//发送关机指令后等待20秒再关闭电源
        //        OffTouYing();
        //        Thread.Sleep(10000);
        //        rRClient.SendHex(DataTypeEnum.LG20005, OrderTypeEnum.Str, PowerOffPC);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20006, OrderTypeEnum.Str, PowerOffPC);
        //        OffTouYing();
        //    });
        //});
    }

    /// <summary>
    /// 投影机
    /// </summary>
    public void OpenAllLED()
    {
        Loom.RunAsync(() =>
        {
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On0A);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On0B);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On0C);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On0D);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On0E);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On0F);
        });
    }

    public void CloseAllLED()
    {
        //ConfirmPanel.ins.Show("确认关闭所有LED?", () =>
        //{
        //    Loom.RunAsync(() =>
        //    {
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off0A);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off0B);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off0C);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off0D);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off0E);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off0F);
        //    });
        //});
    }
    /// <summary>
    /// 主机
    /// </summary>
    public void OpenAllPC()
    {
        Loom.RunAsync(() =>
        {
            rRClient.SendHex(DataTypeEnum.LG20005, OrderTypeEnum.Str, PowerOnPC);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20006, OrderTypeEnum.Str, PowerOnPC);
        });
    }
    public void CloseAllPC()
    {
        //ConfirmPanel.ins.Show("确认关闭所有主机?", () =>
        //{
        //    Loom.RunAsync(() =>
        //    {
        //        rRClient.Send(DataTypeEnum.ShutDown, OrderTypeEnum.ShutDown, "");
        //        Thread.Sleep(10000);//发送关机指令后等待10秒再关闭电源

        //        rRClient.SendHex(DataTypeEnum.LG20005, OrderTypeEnum.Str, PowerOffPC);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20006, OrderTypeEnum.Str, PowerOffPC);
        //    });
        //});

    }

    /// <summary>
    /// 投影
    /// </summary>
    public void OpenAllOneMachine()
    {
        Loom.RunAsync(() =>
        {
            OnTouYing();
        });
    }

    private void OnTouYing()
    {
        rRClient.SendHex(DataTypeEnum.TY1011, OrderTypeEnum.Str, PowerOnTouYing);
        rRClient.SendHex(DataTypeEnum.TY1012, OrderTypeEnum.Str, PowerOnTouYing);
        rRClient.SendHex(DataTypeEnum.TY1013, OrderTypeEnum.Str, PowerOnTouYing);
        rRClient.SendHex(DataTypeEnum.TY1014, OrderTypeEnum.Str, PowerOnTouYing);
        rRClient.SendHex(DataTypeEnum.TY1015, OrderTypeEnum.Str, PowerOnTouYing);
        rRClient.SendHex(DataTypeEnum.TY1016, OrderTypeEnum.Str, PowerOnTouYing);
        rRClient.SendHex(DataTypeEnum.TY1017, OrderTypeEnum.Str, PowerOnTouYing);
        rRClient.SendHex(DataTypeEnum.TY1018, OrderTypeEnum.Str, PowerOnTouYing);
        rRClient.SendHex(DataTypeEnum.TY1019, OrderTypeEnum.Str, PowerOnTouYing);
    }

    public void CloseAllOneMachine()
    {
        //ConfirmPanel.ins.Show("确认关闭所有投影?", () =>
        //{
        //    Loom.RunAsync(() =>
        //    {
        //        OffTouYing();
        //    });
        //});

    }

    private void OffTouYing()
    {
        rRClient.SendHex(DataTypeEnum.TY1011, OrderTypeEnum.Str, PowerOffTouYing);
        rRClient.SendHex(DataTypeEnum.TY1012, OrderTypeEnum.Str, PowerOffTouYing);
        rRClient.SendHex(DataTypeEnum.TY1013, OrderTypeEnum.Str, PowerOffTouYing);
        rRClient.SendHex(DataTypeEnum.TY1014, OrderTypeEnum.Str, PowerOffTouYing);
        rRClient.SendHex(DataTypeEnum.TY1015, OrderTypeEnum.Str, PowerOffTouYing);
        rRClient.SendHex(DataTypeEnum.TY1016, OrderTypeEnum.Str, PowerOffTouYing);
        rRClient.SendHex(DataTypeEnum.TY1017, OrderTypeEnum.Str, PowerOffTouYing);
        rRClient.SendHex(DataTypeEnum.TY1018, OrderTypeEnum.Str, PowerOffTouYing);
        rRClient.SendHex(DataTypeEnum.TY1019, OrderTypeEnum.Str, PowerOffTouYing);
    }

    /// <summary>
    /// 电视机
    /// </summary>
    public void OpenAllTV()
    {
        Loom.RunAsync(() =>
        {
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On01);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On02);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On05);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On06);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On0E);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On0F);

        });
    }
    public void CloseAllTV()
    {
        //ConfirmPanel.ins.Show("关闭所有电视?", () =>
        //{
        //    Loom.RunAsync(() =>
        //    {
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off01);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off02);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off05);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off06);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off0E);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off0F);
        //    });
        //});
    }
    /// <summary>
    /// 灯光
    /// </summary>
    public void OpenAllLight()
    {
        Loom.RunAsync(() =>
        {
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On00);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On04);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On01);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On08);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On02);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On09);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On03);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On0A);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On04);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On0B);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On05);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On0C);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On06);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On0D);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On07);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On10);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On08);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On11);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, On09);

        });
    }
    public void CloseAllLight()
    {
        //ConfirmPanel.ins.Show("确认关闭所有灯光?", () =>
        //{
        //    Loom.RunAsync(() =>
        //    {
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off00);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off04);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off01);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off09);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off02);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off0A);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off03);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off0B);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off05);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off0C);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off06);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off0D);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off07);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off10);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off08);
        //        rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off11);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off09);
        //        Thread.Sleep(sleepTime);
        //        rRClient.SendHex(DataTypeEnum.LG20001, OrderTypeEnum.Str, Off04);
        //    });
        //});

    }

    /// <summary>
    /// 开进门蓝灯
    /// </summary>
    public void OpenLEDBlueLight()
    {
        Loom.RunAsync(() =>
        {
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On09);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, On0A);
        });
    }
    /// <summary>
    /// 关进门蓝灯
    /// </summary>
    public void CloseLEDBlueLight()
    {
        Loom.RunAsync(() =>
        {
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off09);
            Thread.Sleep(sleepTime);
            rRClient.SendHex(DataTypeEnum.LG20002, OrderTypeEnum.Str, Off0A);
        });
    }


}
